//
//  FWGoodsTagView.m
//  FengWoFilterTickerDemo
//
//  Created by 郝秀丽 on 15/10/29.
//  Copyright © 2015年 FengWo. All rights reserved.
//

#import "FWGoodsTagView.h"

#define PI 3.14159265358979323846
#define radius 4.f                  // 小白圆的半径
#define buttonRadius 20.f           // button的半径
#define dx 20.f                     // 斜边x偏移量
#define dy 20.f                     // 斜边y偏移量
#define verticalThreeY 25.f         // 三条横线时横线间距
#define verticalTwoY 18.f           // 两条横线时距离中心点的距离
#define lineWidth 1.2f              // 线的粗细
#define fontHeight 16.f             // 文字的高度
#define fontSize 11.f               // 文字的大小
#define fontHeaderMargin 8.f       // 文字两端的间距(较大的间距)
#define fontTailMargin 5.f          // 文字两端的间距(较小的间距)

typedef NS_ENUM(NSUInteger, branchNumber) {      // 分支的个数
    branchNumberZero,
    branchNumberOne,
    branchNumberTwo,
    branchNumberThree,
};

typedef NS_ENUM(NSUInteger, directionType) {    // 画线的方向
    directionTypeLeft,
    directionTypeRight,
};

typedef NS_ENUM(NSUInteger, appdingStringType) {
    appdingStringTypeGoods,     // 品牌与商品名
    appdingStringTypePrice,     // 币种与价格
    appdingStringTypeAddress,   // 区域与具体购买地址
};

struct FWRang {
    CGFloat minNum;
    CGFloat maxNum;
};
typedef struct FWRang FWRang;


@interface FWGoodsTagView ()

@property (nonatomic, strong) UIButton *circleButton;

@property (nonatomic, assign) FWGoodsTagViewType type;
@property (nonatomic, strong) Model *model;
@property (nonatomic, assign) CGFloat straightLineMaxWidth;     // 直线时最大的宽
@property (nonatomic, assign) CGContextRef context;
@property (nonatomic, assign) branchNumber branchNumber;        // 分支个数
@property (nonatomic, assign) CGPoint centerPoint;              // 中心点
@property (nonatomic, assign) FWRang rangX;                    // X方向上点击触发的有效范围
@property (nonatomic, strong) NSMutableArray *branchTextArrayM; // 分支文本可变数组
@property (nonatomic, assign) CGRect effectiveRect;             // view有效的响应区域

@end

@implementation FWGoodsTagView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.type = FWGoodsTagViewTypeRightBrokenLine;       // 默认值
        self.centerPoint = self.center;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self buildCircleButton];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnGoodsTagView:)];
    [self addGestureRecognizer:tap];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panOnGoodsTagView:)];
    [self addGestureRecognizer:pan];
    
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    /* 文字的计算 */
    // 品牌 商品名
    NSString *stringM0;
    stringM0 = [self appdingTwoStringWithFirstString:self.model.brand SecondString:self.model.productName stringType:appdingStringTypeGoods];
    
    // 币种 价格
    NSString *stringM1;
    stringM1 = [self appdingTwoStringWithFirstString:self.model.price SecondString:self.model.moneyType stringType:appdingStringTypePrice];
    // 国家/... 具体购买地址
    NSString *stringM2;
    stringM2 = [self appdingTwoStringWithFirstString:self.model.country SecondString:self.model.buyAddress stringType:appdingStringTypeAddress];
    
    // 根据stringM0 stringM1 stringM2 判断标签的类型(一支,两支,三支)
    NSMutableArray *branchTextArrayM = [NSMutableArray arrayWithCapacity:3];
    if (![stringM0 isEqualToString:@""]) {
        [branchTextArrayM addObject:stringM0];
    }
    if (![stringM1 isEqualToString:@""]) {
        [branchTextArrayM addObject:stringM1];
    }
    if (![stringM2 isEqualToString:@""]) {
        [branchTextArrayM addObject:stringM2];
    }
    
    switch (branchTextArrayM.count) {
        case 0:
        {
            self.branchNumber = branchNumberZero;
        }
            break;
        case 1:
        {
            self.branchNumber = branchNumberOne;
        }
            break;
        case 2:
        {
            self.branchNumber = branchNumberTwo;
        }
            break;
        case 3:
        {
            self.branchNumber = branchNumberThree;
        }
            break;
            
        default:
            break;
    }
    
    // 初始化context 线的设置
    self.context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(self.context, 1, 1, 1, 1.0);//画笔线的颜色
    CGContextSetLineWidth(self.context, lineWidth);//线的宽度
    
    // 直线型 计算最大的宽度
    // 折线形 计算最大的宽度
    self.effectiveRect = [self rectForGoodsLabelWithStringM0:stringM0 stringM1:stringM1 stringM2:stringM2];
    
    if (self.branchNumber == branchNumberZero) {
        [self removeFromSuperview];
    } else {
        /*画圆*/  //边框圆
        //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度＝180°/π （≈57.3°） 度＝弧度×180°/π 360°＝360×π/180 ＝2π 弧度
        // x,y为圆点坐标，radius半径，startAngle为开始的弧度，endAngle为 结束的弧度，clockwise 0为顺时针，1为逆时针。
        CGContextAddArc(self.context, self.centerPoint.x, self.centerPoint.y, radius, 0, 2*PI, 0); //添加一个圆
        CGContextSetFillColorWithColor(self.context, [UIColor whiteColor].CGColor); //填充的颜色
        CGContextFillPath(self.context); // 开始填充颜色
        CGContextDrawPath(self.context, kCGPathStroke); //绘制路径
        
        if (self.type == FWGoodsTagViewTypeRightBrokenLine || self.type == FWGoodsTagViewTypeLeftBrokenLine) {
            switch (self.branchNumber) {
                case branchNumberOne:
                {
                    /*画线及孤线*/
                    static CGPoint aPoints[2];//坐标点
                    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
                        //右侧下方画线
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x + dx, self.centerPoint.y + dy);//坐标2
                        //CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
                        //points[]坐标数组，和count大小
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[0]];
                    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
                        //左侧下方画线
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x - dx, self.centerPoint.y + dy);//坐标2
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[0]];
                    }
                }
                    break;
                case branchNumberTwo:
                {
                    /*画线及孤线*/
                    static CGPoint aPoints[2];//坐标点
                    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
                        //右侧上方画线
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x + dx, self.centerPoint.y - dy);//坐标2
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[0]];
                        
                        //右侧下方画线
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x + dx, self.centerPoint.y + dy);//坐标2
                        //CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
                        //points[]坐标数组，和count大小
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[1]];
                    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
                        // 左侧上方画线
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x - dx, self.centerPoint.y - dy);//坐标2
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[0]];
                        //左侧下方画线
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x - dx, self.centerPoint.y + dy);//坐标2
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[1]];
                    }
                    
                }
                    break;
                case branchNumberThree:
                {
                    /*画线及孤线*/
                    static CGPoint aPoints[2];//坐标点
                    
                    //右侧下方画线
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    aPoints[1] =CGPointMake(self.centerPoint.x + dx, self.centerPoint.y + dy);//坐标2
                    //CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
                    //points[]坐标数组，和count大小
                    CGContextAddLines(self.context, aPoints, 2);//添加线
                    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[2]];
                    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[1]];
                    }
                    
                    //右侧上方画线
                    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x + dx, self.centerPoint.y - dy);//坐标2
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[0]];
                    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
                        
                    }
                    
                    //左侧下方画线
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    aPoints[1] =CGPointMake(self.centerPoint.x - dx, self.centerPoint.y + dy);//坐标2
                    CGContextAddLines(self.context, aPoints, 2);//添加线
                    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[1]];
                    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[2]];
                    }
                    
                    // 左侧上方画线
                    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
                        
                    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
                        aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                        aPoints[1] =CGPointMake(self.centerPoint.x - dx, self.centerPoint.y - dy);//坐标2
                        CGContextAddLines(self.context, aPoints, 2);//添加线
                        CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[0]];
                    }
                }
                    break;
                    
                    
                default:
                    break;
            }
            
            
        } else if (self.type == FWGoodsTagViewTypeRightStraightLine || self.type == FWGoodsTagViewTypeLeftStraightLine) {
            
            CGPoint aPoints[2];//坐标点
            switch (self.branchNumber) {
                case branchNumberOne:
                {
                    //中间
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    if (self.type == FWGoodsTagViewTypeRightStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[0] directionType:directionTypeRight text:branchTextArrayM[0]];
                    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[0] directionType:directionTypeLeft text:branchTextArrayM[0]];
                    }
                }
                    break;
                case branchNumberTwo:
                {
                    //上方画线
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    aPoints[1] =CGPointMake(self.centerPoint.x, self.centerPoint.y - verticalTwoY);//坐标2
                    CGContextAddLines(self.context, aPoints, 2);//添加线
                    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                    if (self.type == FWGoodsTagViewTypeRightStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[0]];
                    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[0]];
                    }
                    
                    //下方画线
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    aPoints[1] =CGPointMake(self.centerPoint.x, self.centerPoint.y + verticalTwoY);//坐标2
                    CGContextAddLines(self.context, aPoints, 2);//添加线
                    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                    if (self.type == FWGoodsTagViewTypeRightStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:branchTextArrayM[1]];
                    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:branchTextArrayM[1]];
                    }
                    
                    
                }
                    break;
                case branchNumberThree:
                {
                    //上方画线
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    aPoints[1] =CGPointMake(self.centerPoint.x, self.centerPoint.y - verticalThreeY);//坐标2
                    CGContextAddLines(self.context, aPoints, 2);//添加线
                    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                    if (self.type == FWGoodsTagViewTypeRightStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:stringM0];
                    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:stringM0];
                    }
                    
                    //中间
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    if (self.type == FWGoodsTagViewTypeRightStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[0] directionType:directionTypeRight text:stringM1];
                    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[0] directionType:directionTypeLeft text:stringM1];
                    }
                    
                    //下方画线
                    aPoints[0] =CGPointMake(self.centerPoint.x, self.centerPoint.y);//坐标1
                    aPoints[1] =CGPointMake(self.centerPoint.x, self.centerPoint.y + verticalThreeY);//坐标2
                    CGContextAddLines(self.context, aPoints, 2);//添加线
                    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
                    if (self.type == FWGoodsTagViewTypeRightStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeRight text:stringM2];
                    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
                        [self drawLineAndTextWithOrginPoint:aPoints[1] directionType:directionTypeLeft text:stringM2];
                    }
                }
                    break;
                    
                default:
                    break;
            }
        }
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *result;
    result = [super hitTest:point withEvent:event];
    BOOL isContains;
    isContains = CGRectContainsPoint(self.effectiveRect, point);
    return isContains? result:nil;
    
}

#pragma mark - Private Else
- (CGRect)rectForGoodsLabelWithStringM0:(NSString *)stringM0 stringM1:(NSString *)stringM1 stringM2:(NSString *)stringM2 {
    
    CGSize size = CGSizeMake(MAXFLOAT, fontHeight);
    CGFloat width0 = 0.f;
    width0 = [stringM0 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
    CGFloat width1 = 0.f;
    width1 = [stringM1 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
    CGFloat width2 = 0.f;
    width2 = [stringM2 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
    
    if (self.branchNumber == branchNumberZero) {
        return self.effectiveRect;
    }
    
    // 计算width0 width1 width2 的最大值
    CGFloat maxInThree = width0 > width1 ? width0 : width1;
    maxInThree = maxInThree > width2 ? maxInThree :width2;
    
    CGFloat maxInTwo = width0 > width2 ? width0 : width2;
    
    FWRang rangX;
    CGFloat effectiveHeight = 4 * verticalThreeY;
    switch (self.type) {
        case FWGoodsTagViewTypeRightBrokenLine:
        {
            if (self.branchNumber == branchNumberOne) {
                rangX.minNum = self.centerPoint.x - buttonRadius;
                rangX.maxNum = self.centerPoint.x + (maxInThree + dx + fontHeaderMargin + fontTailMargin);
                effectiveHeight = 2 * verticalThreeY;
            } else if (self.branchNumber == branchNumberTwo) {
                rangX.minNum = self.centerPoint.x - buttonRadius;
                rangX.maxNum = self.centerPoint.x + (maxInThree + dx + fontHeaderMargin + fontTailMargin);
                effectiveHeight = 3 * verticalThreeY;
            } else if (self.branchNumber == branchNumberThree) {
                rangX.minNum = self.centerPoint.x - (width1 + dx + fontHeaderMargin + fontTailMargin);
                rangX.maxNum = self.centerPoint.x + (maxInTwo + dx + fontHeaderMargin + fontTailMargin);
                effectiveHeight = 3 * verticalThreeY;
            }
        }
            break;
        case FWGoodsTagViewTypeLeftBrokenLine:
        {
            if (self.branchNumber == branchNumberOne) {
                rangX.minNum = self.centerPoint.x - (maxInThree + dx + fontHeaderMargin + fontTailMargin);
                rangX.maxNum = self.centerPoint.x + buttonRadius;
                effectiveHeight = 2 * verticalThreeY;
            } else if (self.branchNumber == branchNumberTwo) {
                rangX.minNum = self.centerPoint.x - (maxInThree + dx + fontHeaderMargin + fontTailMargin);
                rangX.maxNum = self.centerPoint.x + buttonRadius;
                effectiveHeight = 3 * verticalThreeY;
            } else if (self.branchNumber == branchNumberThree) {
                rangX.minNum = self.centerPoint.x - (maxInTwo + dx + fontHeaderMargin + fontTailMargin);
                rangX.maxNum = self.centerPoint.x + (width1 + dx + fontHeaderMargin + fontTailMargin) ;
                effectiveHeight = 3 * verticalThreeY;
            }
        }
            break;
        case FWGoodsTagViewTypeRightStraightLine:
        {
            rangX.minNum = self.centerPoint.x - buttonRadius;
            rangX.maxNum = self.centerPoint.x + (maxInThree + fontHeaderMargin + fontTailMargin);
            if (self.branchNumber == branchNumberOne) {
                effectiveHeight = 2 * verticalThreeY;
            } else if (self.branchNumber == branchNumberTwo) {
                effectiveHeight = 3 * verticalThreeY;
            } else if (self.branchNumber == branchNumberThree) {
                effectiveHeight = 4 * verticalThreeY;
            }
        }
            break;
        case FWGoodsTagViewTypeLeftStraightLine:
        {
            rangX.minNum = self.centerPoint.x - (maxInThree + fontHeaderMargin + fontTailMargin);
            rangX.maxNum = self.centerPoint.x + buttonRadius;
            if (self.branchNumber == branchNumberOne) {
                effectiveHeight = 2 * verticalThreeY;
            } else if (self.branchNumber == branchNumberTwo) {
                effectiveHeight = 3 * verticalThreeY;
            } else if (self.branchNumber == branchNumberThree) {
                effectiveHeight = 4 * verticalThreeY;
            }
            
        }
            break;
            
        default:
            break;
    }
    self.effectiveRect = CGRectMake(rangX.minNum, self.centerPoint.y - effectiveHeight / 2, rangX.maxNum - rangX.minNum, effectiveHeight);
    
    // 调试代码  显示effectiveRect区域
    //    CGContextSetLineWidth(self.context, 2.0);
    //    CGContextSetStrokeColorWithColor(self.context, [UIColor blueColor].CGColor);
    //    CGContextMoveToPoint(self.context, rangX.minNum, self.centerPoint.y - effectiveHeight / 2);
    //    CGContextAddLineToPoint(self.context, rangX.minNum + (rangX.maxNum - rangX.minNum), self.centerPoint.y - effectiveHeight / 2);
    //    CGContextAddLineToPoint(self.context, rangX.minNum + (rangX.maxNum - rangX.minNum), self.centerPoint.y - effectiveHeight / 2 + effectiveHeight);
    //    CGContextAddLineToPoint(self.context, rangX.minNum, self.centerPoint.y - effectiveHeight / 2 + effectiveHeight);
    //    CGContextAddLineToPoint(self.context, rangX.minNum, self.centerPoint.y - effectiveHeight / 2);
    //    CGContextStrokePath(self.context);
    
    return self.effectiveRect;
}
// 拼接两个字段
- (NSString *)appdingTwoStringWithFirstString:(NSString *)firstString SecondString:(NSString *)secondString stringType:(appdingStringType)stringType {
    // 注意传参时  firstString:价格  secondString:币种
    NSMutableString *stringM;
    if (![firstString isEqualToString:@""] && [secondString isEqualToString:@""]) {
        if (stringType == appdingStringTypePrice) {
            stringM = [NSMutableString stringWithFormat:@"%@ 人民币",firstString];
        } else {
            stringM = [NSMutableString stringWithString:firstString];
        }
        
    } else if ([firstString isEqualToString:@""] && ![secondString isEqualToString:@""]) {
        if (stringType == appdingStringTypePrice) {
            stringM = [NSMutableString stringWithString:@""];
        } else {
            stringM = [NSMutableString stringWithString:secondString];
        }
    } else if ([firstString isEqualToString:@""] && [secondString isEqualToString:@""]) {
        stringM = [NSMutableString stringWithString:@""];
    } else if (![firstString isEqualToString:@""] && ![secondString isEqualToString:@""]) {
        stringM = [NSMutableString stringWithString:firstString];
        [stringM appendFormat:@" %@",secondString];
    }
    NSString *string = [NSString stringWithString:stringM];
    return string;
}

// 画左右直线以及上面的文字
- (void)drawLineAndTextWithOrginPoint:(CGPoint)orginPoint directionType:(directionType)directionType text:(NSString *)text {
    static CGPoint aPoints[2];//坐标点
    
    aPoints[0] = CGPointMake(orginPoint.x, orginPoint.y);  // 坐标1
    CGSize size = CGSizeMake(MAXFLOAT, fontHeight);
    CGFloat width = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
    if (directionType == directionTypeLeft) {
        aPoints[1] = CGPointMake(orginPoint.x - fontHeaderMargin - width - fontTailMargin, orginPoint.y);
        [text drawWithRect:CGRectMake(orginPoint.x - fontHeaderMargin - width, orginPoint.y - fontHeight, width, fontHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize], NSForegroundColorAttributeName : [UIColor whiteColor]} context:nil];
    } else if (directionType == directionTypeRight) {
        aPoints[1] = CGPointMake(orginPoint.x + fontHeaderMargin + width + fontTailMargin, orginPoint.y);
        [text drawWithRect:CGRectMake(orginPoint.x + fontHeaderMargin, orginPoint.y - fontHeight, width, fontHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName : [UIColor whiteColor]} context:nil];
    }
    CGContextAddLines(self.context, aPoints, 2);//添加线
    CGContextDrawPath(self.context, kCGPathStroke); //根据坐标绘制路径
}

#pragma mark - Public
- (void)reloadWithModel:(Model *)model {
    self.model = model;
    
    [self setNeedsDisplay];
}

+ (CGRect)rectForCustomLabelViewwithModel:(Model *)model {
    CGRect rect;
    return rect;
}

#pragma mark - Builder
- (void)buildCircleButton {
    self.circleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.circleButton.backgroundColor = [UIColor clearColor];
    self.circleButton.frame = CGRectMake(self.centerPoint.x - buttonRadius, self.centerPoint.y - buttonRadius, buttonRadius * 2, buttonRadius * 2);
    self.circleButton.layer.cornerRadius = buttonRadius;
    [self.circleButton addTarget:self action:@selector(didclickCircleButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.circleButton];
}

#pragma mark - SelectorMethod
- (void)didclickCircleButton:(UIButton *)button {
    if (self.type == FWGoodsTagViewTypeRightBrokenLine) {
        self.type = FWGoodsTagViewTypeLeftBrokenLine;
        
    } else if (self.type == FWGoodsTagViewTypeLeftBrokenLine) {
        self.type = FWGoodsTagViewTypeRightStraightLine;
        
    } else if (self.type == FWGoodsTagViewTypeRightStraightLine) {
        self.type = FWGoodsTagViewTypeLeftStraightLine;
        
    } else if (self.type == FWGoodsTagViewTypeLeftStraightLine) {
        self.type = FWGoodsTagViewTypeRightBrokenLine;
    }
    else{
        self.type = FWGoodsTagViewTypeRightBrokenLine;
    }
    [self setNeedsDisplay];
}

- (void)tapOnGoodsTagView:(UITapGestureRecognizer *)tap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(goodsTagView:tapWithModel:)]) {
        [self.delegate goodsTagView:self tapWithModel:self.model];
    }
}

- (void)panOnGoodsTagView:(UIPanGestureRecognizer *)pan {
    CGPoint point = [pan translationInView:pan.view];
    
    CGFloat width = self.superview.bounds.size.width;
    CGFloat height = self.superview.bounds.size.height;
    
    CGPoint pointTemp;
    if (pan.view.center.x + point.x < 0) {
        pointTemp.x = 0;
    } else if (pan.view.center.x + point.x > width) {
        pointTemp.x = width;
    } else {
        pointTemp.x = pan.view.center.x + point.x;
    }
    
    if (pan.view.center.y + point.y < 0) {
        pointTemp.y = 0;
    } else if (pan.view.center.y + point.y > height) {
        pointTemp.y = height;
    } else {
        pointTemp.y = pan.view.center.y + point.y;
    }
    
    pan.view.center = CGPointMake(pointTemp.x, pointTemp.y);
    [pan setTranslation:CGPointMake(0, 0) inView:[pan.view superview]];
}

@end

